import { Component, ViewChild,OnInit } from '@angular/core';
import {Msg} from "../../../providers/global";
import {MsgService} from "../../../providers/message.service";
import * as moment from 'moment';
import { Router } from "@angular/router";
import {DataScroller} from 'primeng/primeng';
@Component({
  selector: 'app-message',
  templateUrl: './message.component.html',
  styleUrls: ['./message.component.scss']
})

export class MessageComponent implements OnInit {
  @ViewChild(DataScroller) dataTableComponent: DataScroller;
  
  msg;
  msgs:Msg[];
  constructor(public msgService:MsgService,public router:Router) { 
   
  }

  ngOnInit() {
    this.init();
  }
  getJSON() {
    return new Promise((resolve, reject) => {
      this.msgService.getMsg().subscribe(data => {
        try {
          this.msgs = <Msg[]>data.json();
          resolve();
        } catch (e) {
          reject();
        }
      });
    })
  }

  async init() {
    try {
      await this.getJSON();
      setTimeout(() => {
        this.dataTableComponent.reset(); 
      }, 150);
      this.msg = '';
    } catch (err) {
      await this.init();
    }
  }
  click(){
    if(this.msg!==''){
      this.msgService.postMsg(this.msg).subscribe(data=>{
        this.init();
      })
    }else{
      console.log('null')
    }
    
  }

}
